package com.cskt.petproject178.controller;

import com.cskt.petproject178.pojo.Authened;
import com.cskt.petproject178.pojo.entity.ReceiverAddress;
import com.cskt.petproject178.pojo.vo.ResultVO;
import com.cskt.petproject178.pojo.vo.StatusEnum;
import com.cskt.petproject178.pojo.vo.UserInfoVO;
import com.cskt.petproject178.service.AddressService;
import com.cskt.petproject178.service.PcdDicService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 地址控制器
 *
 * @author 27013
 **/

@RestController
@RequestMapping(value = "/ums")
public class AddressController {
    private static  final Logger log= LoggerFactory.getLogger(AddressController.class);
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private PcdDicService pcdDicService;
    @Resource
    private AddressService addressService;
    @GetMapping(value = "/receiverAddress/pcdList")
    public ResultVO<?> addressList(){
        return ResultVO.OK(pcdDicService.selectPcdDicList());
    }

    @PostMapping(value = "/receiverAddress/saveOrUpdate")
    @Authened
    public ResultVO<?> saveOrUpdateAddress(@RequestParam String userName,
                                           @RequestParam String phoneNumber,
                                           @RequestParam Integer defaultStatus,
                                           @RequestParam Long countyId,
                                           @RequestParam String detailAddress,
                                           @RequestParam(required = false) Long id,
                                           @RequestHeader String Token) {
        try {
            ReceiverAddress address = new ReceiverAddress();
            String userStr = stringRedisTemplate.opsForValue().get("pet:token:" + Token);
            UserInfoVO userInfoVO = objectMapper.readValue(userStr, UserInfoVO.class);
            address.setUserId(userInfoVO.getId());
            address.setUserName(userName);
            address.setPhoneNumber(phoneNumber);
            address.setDefaultStatus(defaultStatus);
            address.setCountyId(countyId);
            address.setDetailAddress(detailAddress);
            address.setCreatedTime(LocalDateTime.now());
            address.setUpdatedTime(LocalDateTime.now());
            address.setIsDelete(false);
            address.setId(id);
            boolean result = addressService.updateByPrimaryKeySelective(address);
            //如果修改失败，则默认做新增操作
            if (!result) {
                 result = addressService.insertSelective(address);
            }
            return result?ResultVO.OK():ResultVO.ERR(StatusEnum.COMMON_EXCEPTION);
        } catch (JsonProcessingException e) {
            log.error("做添加地址操作获取用户编号时反序列化出错",e);
           return  ResultVO.ERR(StatusEnum.COMMON_EXCEPTION);
        }
    }

    }

